package com.example.myspringbootpj.mapper;

import com.example.myspringbootpj.entity.User;
import com.example.myspringbootpj.entity.Word;
import org.apache.ibatis.annotations.*;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {


    @Insert("INSERT INTO user (user_id, user_password, user_name, user_image) VALUES (#{user_id}, #{user_password}, #{user_name}, #{user_image})")
    int add(User user);

    @Select("SELECT COUNT(*) FROM user WHERE user_id = #{user_id}")
    int countByUserId(@Param("user_id") String userId);
//    // 插入
//    @Insert("insert into user (user_id, user_password, user_name, user_image) values (#{user_id},#{user_password},#{user_name},#{user_image})")
//    int add(User user);
//
//    @Select("SELECT COUNT(*) FROM user WHERE user_id = #{user_id}")
//    int countByUserId(@Param("user_id") String userId);


    // 根据用户编号和密码查询用户信息，如果存在匹配的用户则返回该用户对象，否则返回null
    @Select("SELECT * FROM user WHERE user_id = #{user_id} AND user_password = #{user_password}")
    @Results({
            @Result(column = "user_id", property = "user_id"),
            @Result(column = "user_password", property = "user_password")
    })
    User selectByUserIdAndPassword(String user_id, String user_password);

    //查询用户是否封号
    @Select("""
SELECT user_id,user_not_use from user WHERE user_id = #{user_id} AND user_password = #{user_password}
""")
    @Results({
            @Result(column = "user_id", property = "user_id"),
            @Result(column = "user_password", property = "user_password"),
            @Result(column = "user_not_use", property = "user_not_use"),


    })
    User findBan(@Param("user_id") String userId,@Param("user_password")String user_password);

    //封号
    @Update("""
update user set user_not_use=#{user_not_use} where user_id=#{user_id}""")
    void setUserBan(@Param("user_id") String userId, @Param("user_not_use") LocalDate user_not_use);

    //根据头像获取id再通过id获取用户
    @Select("select user_image  from user where user_id = #{user_id}")
    String selectUserImage(String user_id);

    //点击头像获取用户信息
    @Select("select user_image,user_name,user_kubi from user where user_id=#{user_id}")
    @Results({
            @Result(column = "user_image", property = "user_image"),
            @Result(column = "user_name", property = "user_name"),
            @Result(column = "user_kubi", property = "user_kubi"),
//            @Result(column = "user_id", property = "user_id"),
    })
    User selectUserById(String user_id);

    //更改用户姓名
    @Update("UPDATE user SET user_name = #{user_name} WHERE user_id = #{user_id}")
    int updateNaById(@Param("user_id") String userId, @Param("user_name") String userName);

    //更改用户头像
    @Update("UPDATE user SET user_image = #{user_image} WHERE user_id = #{user_id}")
    int updateImById(@Param("user_id") String userId, @Param("user_image") String userImage);

    //更改用户密码
    @Update("UPDATE user SET user_password = #{user_password} WHERE user_id = #{user_id}")
    int updatePsById(@Param("user_id") String userId, @Param("user_password") String userPassword);

    //管理元通过username模糊查询
    @Select("""
select user_id,user_name,user_kubi,user_password,user_not_use,user_image from user where user_name like concat('%',#{user_id},'%') or user_id like concat('%',#{user_id},'%')""")
    @Results({
            @Result(column = "user_password", property = "user_password"),
            @Result(column = "user_name", property = "user_name"),
            @Result(column = "user_id", property = "user_id"),
            @Result(column = "user_image", property = "user_image"),
            @Result(column = "user_not_use", property = "user_not_use")


    })
    List<User> selectUserList(@Param("user_id") String user_id);

    //增加酷币
    @Update("""
UPDATE user 
SET user_kubi = user_kubi + #{user_kubi}
WHERE user_id = #{user_id}
""")
    void updateUserKubi(@Param("user_id") String user_id, @Param("user_kubi") int user_kubi);

//封禁用户数量
    @Select("""
select count(*) as total_count from user where user_not_use > #{user_not_use}""")
    @Results({
            @Result(property = "count", column = "total_count")
    })
    int selectAllBan(@Param("user_not_use") LocalDate user_not_use);

    //用户总数
    @Select("""
select count(*) from user""")
    int selectAllUserCount();

//写笔记
    @Insert("""
insert into note(user_id, word_id, word_note) values (#{user_id},#{word_id},#{word_note})""")
    void addNote(@Param("user_id") String userId, @Param("word_id") Integer wordId, @Param("word_note") String note);
    //看笔记
    @Select("""
    SELECT
        w.word_text,w.word_id
    FROM
        note n
    INNER JOIN
        word w
    ON
        n.word_id = w.word_id
    WHERE
        n.user_id = #{userId}
""")
    @Results({@Result(property = "word_text" , column = "word_text"),
    @Result(property = "word_id" ,column = "word_id")})
     List<Word> selectWordNote(@Param("userId") String userId);

    @Select("""
SELECT word_note
FROM note
WHERE user_id = #{userId} AND word_id = #{wordId}
""")@Results(@Result(column = "word_note",property = "word_note"))
    String selectNoteByWordIdAndUserId(@Param("userId") String userId, @Param("wordId") String wordId);


}
